<template>
  <div>
    <city-header></city-header>
    <city-search :cities="cities"></city-search>
    <city-list :cities="cities" :hot="hotCities" :letter="letter"></city-list>
    <city-alphabet :cities="cities" @change="handleLetterChange"></city-alphabet>
  </div>
</template>

<script>
import axios from "axios";
import CityHeader from "./components/Header";
import CitySearch from "./components/Search";
import CityList from "./components/List";
import CityAlphabet from "./components/Alphabet";
export default {
  name: "City",
  data() {
    return {
      cities: {},
      hotCities: [],
      letter:'',
    };
  },
  components: {
    CityHeader,
    CitySearch,
    CityList,
    CityAlphabet,
  },
  methods: {
    getCityInfo() {
      axios
        .get(
          "https://www.fastmock.site/mock/4c7a35990c15d8e6700c84cdc193ecc9/api/city"
        )
        .then((res) => this.handleGetCityInfoSucc(res));
    },
    handleGetCityInfoSucc(res) {
      console.log(res);
      res=res.data;
      if(res.ret&&res.data) {
          const data=res.data;
          this.cities=data.cities;
          this.hotCities=data.hotCities;
      }
    },
    // 将letter赋值为在CityAlphabet组件中被点击的字母
    handleLetterChange(letter){
      this.letter=letter;
    }
  },
  // 当页面挂载完毕后执行getCityInfo()函数，发送请求
  mounted() {
    this.getCityInfo();
  },
};
</script>

<style lang="scss" scoped>
</style>